package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Basket;
import com.powernode.model.CartVo;
import com.powernode.model.Result;
import com.powernode.service.BasketService;
import com.powernode.util.AuthUtils;
import com.powernode.vo.CartTotalAmount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @create: 2024/7/25
 * @author: gyhe10
 * @description: 购物车业务控制层
 **/

@Api(tags = "购物车业务接口管理")
@RestController
@RequestMapping("p/shopCart")
public class BasketController {

    @Resource
    private BasketService basketService;

    /**
     * 查询会员购物车中商品数量
     * @return
     */
    @ApiOperation("查询会员购物车中商品数量")
    @GetMapping("prodCount")
    public Result<Integer> loadMemberBasketProdCount() {
        // 获取当前会员的openId
        String openId = AuthUtils.getMemberOpenId();
        Integer count = basketService.queryMemberBasketProdCount(openId);
        return Result.success(count);
    }

    /**
     * 查询会员购物车页面数据
     * @return
     */
    @ApiOperation("查询会员购物车页面数据")
    @GetMapping("info")
    public Result<CartVo> loadMemberCartVo() {
        // 获取当前会员的openId
        String openId = AuthUtils.getMemberOpenId();
        CartVo cartVo = basketService.queryMemberCartVo(openId);
        return Result.success(cartVo);
    }

    /**
     * 计算购物车中会员选中商品的金额
     * @param basketIdList 会员选中的购物车记录ID集合
     * @return
     */
    @ApiOperation("计算购物车中会员选中商品的金额")
    @PostMapping("totalPay")
    public Result<CartTotalAmount> calculateMemberCheckedBasketTotalAmount(@RequestBody List<Long> basketIdList) {
        CartTotalAmount cartTotalAmount = basketService.calculateMemberCheckedBasketTotalAmount(basketIdList);
        return Result.success(cartTotalAmount);
    }

    /**
     * 添加商品到购物车/修改购物车中商品数量
     * @param basket 购物车记录对象，封装前端传入的prodCount、shopId、skuId、prodId
     * @return
     */
    @ApiOperation("添加商品到购物车/修改购物车中商品数量")
    @PostMapping("changeItem")
    public Result<String> changeCartItem(@RequestBody Basket basket) {
        Boolean changed = basketService.changeCartItem(basket);
        return Result.handle(changed);
    }

    /**
     * 删除会员选中的购物车记录
     * @param basketIdList 会员选中的购物车记录ID集合
     * @return
     */
    @ApiOperation("删除会员选中的购物车记录")
    @DeleteMapping("deleteItem")
    public Result<String> removeMemberCheckedBasketItem(@RequestBody List<Long> basketIdList) {
        boolean removed = basketService.removeBatchByIds(basketIdList);
        return Result.handle(removed);
    }


    //////////// feign接口 ////////////

    /**
     * 根据购物车记录ID集合查询购物车记录集合
     * @param basketIdList
     * @return
     */
    @GetMapping("getBasketListByIds")
    public Result<List<Basket>> getBasketListByIds(@RequestParam List<Long> basketIdList) {
        List<Basket> basketList = basketService.listByIds(basketIdList);
        return Result.success(basketList);
    }

    /**
     * 根据会员openId和skuId集合删除购物车记录
     * @param param
     * @return
     */
    @DeleteMapping("removeBasketByOpenIdAndSkuIds")
    public Result<Boolean> removeBasketByOpenIdAndSkuIds(@RequestBody Map<String, Object> param) {
        // 获取会员openId
        String openId = (String) param.get("openId");
        // 获取商品skuId集合
        List<Long> skuIdList = (List<Long>) param.get("skuIdList");
        // 根据会员openId和skuId集合删除购物车记录
        boolean removed = basketService.remove(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getOpenId, openId)
                .in(Basket::getSkuId, skuIdList));
        return Result.success(removed);
    }

}
